package com.java.teachercode.mycode_2;

import javax.swing.*;

/**
 * @author shkstart
 * @create 2021-01-28 16:15
 */
public class CountTest extends JFrame implements Runnable {
    private JButton[] jButton;
    private JTextField seeCount;
    int hour;
    int minutes;
    int mouse;
    CountTest countTest = new CountTest();
    Thread thread = new Thread(countTest);

    public CountTest() {

        this.setTitle("计时器");
        this.setSize(400, 300);
        this.setLocationRelativeTo(null);
        //设置布局
        this.setLayout(null);
        //添加组件
        addComponent();
        thread.start();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private void addComponent() {
        String[] btnStr = {"开始", "重置"};
        //添加按钮
        this.jButton = new JButton[2];
        for (int i = 0; i < jButton.length; i++) {
            this.jButton[i] = new JButton(btnStr[i]);
            this.jButton[i].setBounds((200 * i) + 50, 180, 100, 30);
            this.add(this.jButton[i]);
        }
        seeCount = new JTextField();
        seeCount.setBounds(120, 50, 150, 30);
        this.add(seeCount);

    }

    public JButton[] getjButton() {
        return jButton;
    }

    public void setjButton(JButton[] jButton) {
        this.jButton = jButton;
    }

    public JTextField getSeeCount() {
        return seeCount;
    }

    public void setSeeCount(JTextField seeCount) {
        this.seeCount = seeCount;
    }

    @Override
    public void run() {
        if (jButton[0].getText().equals("开始")) {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mouse += 1;
                if (mouse == 60) {
                    minutes += 1;
                    mouse = 0;
                    if (minutes == 60) {
                        hour += 1;
                        minutes = 0;
                    }
                }
                seeCount.setText(hour + "时" + minutes + "分" + mouse + "秒");
            }

        } else if (jButton[1].getText().equals("重置")) {
            hour = 0;
            minutes = 0;
            mouse = 0;
            seeCount.setText(hour + "时" + minutes + "分" + mouse + "秒");
        }
    }
}
